﻿using System;
using System.Web;
using System.Web.SessionState;

using DreamCube.Foundation.Basic.Objects;
using DreamCube.Foundation.Basic.Utility;

namespace DreamCube.Websites.BLL
{
    public class WebSession
    {
        /// <summary>
        /// 登陆进入第一个系统
        /// （系统自动读取网站的指定目录的XML配置文件 ~/Common/Configs/Systems.xml）
        /// </summary>
        /// <returns></returns>
        public static Boolean LoginFirstSystem()
        {
            String xmlPath = HttpContext.Current.Server.MapPath("~/Common/Configs/Systems.xml");
            return LoginFirstSystem(xmlPath);
        }

        /// <summary>
        /// 登陆进入第一个系统
        /// </summary>
        /// <param name="systemConfigFilePath">系统配置的XML文件路径</param>
        /// <returns></returns>
        public static Boolean LoginFirstSystem(String systemConfigFilePath)
        {
            try
            {
                BLL.WebSession.CurrentSystem = BLL.WebSystem.GetFirstSystem(systemConfigFilePath);
                return true;
            }
            catch (Exception ex)
            {
                MyLog.MakeLog(ex);
                return false;
            }
        }

        /// <summary>
        /// 根据系统的ID，登陆进某个系统
        /// </summary>
        /// <param name="systemConfigFilePath">系统配置的XML文件路径</param>
        /// <param name="systemID"></param>
        /// <returns></returns>
        public static Boolean LoginSystem(String systemConfigFilePath, String systemID)
        {
            try
            {
                if (!String.IsNullOrEmpty(systemID))
                {
                    BLL.WebSession.CurrentSystem = BLL.WebSystem.GetSystem(systemConfigFilePath, systemID);
                }
                else
                {
                    BLL.WebSession.CurrentSystem = BLL.WebSystem.GetFirstSystem(systemConfigFilePath);
                }
                return true;
            }
            catch (Exception ex)
            {
                MyLog.MakeLog(ex);
                return false;
            }
        }

        /// <summary>
        /// 当前登陆的用户名
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userPassword"></param>
        /// <returns></returns>
        public static FuncResult LoginUser(String userName, String userPassword)
        {
            try
            {
                BLL.WebSession.CurrentUserName = userName;
                BLL.WebSession.CurrentUserPassword = userPassword;
                return new FuncResult { ErrorMsg = "登陆成功", Status = true };
            }
            catch (Exception ex)
            {
                MyLog.MakeLog(ex);
                return new FuncResult { ErrorMsg = "登陆失败！", Status = false };
            }
        }

        /// <summary>
        /// 获取当前登录的系统
        /// </summary>
        public static WebSystem CurrentSystem
        {
            get
            {
                return MySession.Get<WebSystem>("webSystem");
            }
            internal set
            {
                MySession.Set("webSystem", value);
            }
        }

        /// <summary>
        /// 当前登录的用户名
        /// </summary>
        public static String CurrentUserName
        {
            get;
            internal set;
        }

        /// <summary>
        /// 当前登录的密码
        /// </summary>
        public static String CurrentUserPassword
        {
            get;
            internal set;
        }
    }
}
